package com.ihealthway.all;

import com.ihealthway.all.groupbuy.services.MPSettingService;
import com.ihealthway.all.task.AsyncOperateLogTask;
import com.ihealthway.all.groupbuy.vo.MPSetting;
import io.github.elkan1788.mpsdk4j.vo.MPAccount;
import org.nutz.integration.quartz.NutQuartzCronJobFactory;
import org.nutz.ioc.Ioc;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

/**
 * Load necessary conf or start service
 *
 * @author 凡梦星尘(elkan1788@gmail.com)
 */
public class StartSetup implements Setup {

    private static final Log logger = Logs.get();

    private Ioc ioc;

    @Override
    public void init(NutConfig nc) {
        this.ioc = nc.getIoc();
        // Star async admin operate log
        this.ioc.get(AsyncOperateLogTask.class);

        MPSettingService mpSetService = this.ioc.get(MPSettingService.class);
        MPSetting mpSet = mpSetService.get();
        MPAccount mpAct = this.ioc.get(MPAccount.class,"mpAct");
        mpSet.cloneToMPAct(mpAct);
        if (mpAct != null) {
            mpAct = this.ioc.get(MPAccount.class, "mpAct");
            logger.infof("MP account info, mpId: %s, appId: %s, appSecret: %s, devToken: %s",
                    mpAct.getMpId(), mpAct.getAppId(),
                    Strings.brief(mpAct.getAppSecret(), 16), mpAct.getToken());

            mpSetService.refreshWechatAllToken();
        }

        // Start quartz jobs
        this.ioc.get(NutQuartzCronJobFactory.class);

    }

    @Override
    public void destroy(NutConfig nc) {
    }
}
